package org.dizem.groupon.service.impl;

import org.dizem.groupon.Constants;
import org.dizem.groupon.dao.CityDAO;
import org.dizem.groupon.dao.UserDAO;
import org.dizem.groupon.model.domain.CityEntity;
import org.dizem.groupon.model.domain.UserEntity;
import org.dizem.groupon.service.UserService;
import org.dizem.util.MD5Util;
import org.dizem.util.MailUtil;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * User: dizem@126.com
 * Time: 11-6-27 上午10:58
 */
public class UserServiceImpl implements UserService {
	private UserDAO userDAO;
	private CityDAO cityDAO;

	public CityDAO getCityDAO() {
		return cityDAO;
	}

	public void setCityDAO(CityDAO cityDAO) {
		this.cityDAO = cityDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public boolean usernameOrEmailExists(String usernameOrEmail) {
		return userDAO.getByUsername(usernameOrEmail) != null ||
				userDAO.getByEmail(usernameOrEmail) != null;
	}

	public boolean validateByUsername(String username, String password) {
		UserEntity user = userDAO.getByUsername(username);
		String md5 = MD5Util.encode(password);
		return user != null && user.getPassword().equals(md5);
	}

	public boolean validateByEmail(String email, String password) {
		UserEntity user = userDAO.getByEmail(email);
		String md5 = MD5Util.encode(password);
		return user != null && user.getPassword().equals(md5);
	}

	public boolean isUsernameExisted(String username) {
		return userDAO.isUsernameExisted(username);
	}

	public boolean isEmailExisted(String email) {
		return userDAO.isEmailExisted(email);
	}

	public void register(UserEntity user, String cityName) {
		CityEntity city = cityDAO.getByChnName(cityName);
		user.setAccountType(Constants.USER_TYPE.NOT_AUTHED.ordinal());
		user.setAuthCode(UUID.randomUUID().toString());
		user.setCity(city);
		user.setPassword(MD5Util.encode(user.getPassword()));
		user.setRegisterTime(new Date());
		MailUtil.sendEmail(user.getEmail(), user.getUsername(), user.getAuthCode());
		userDAO.save(user);
	}

	public UserEntity getByUsername(String username) {
		return userDAO.getByUsername(username);
	}

	public List<UserEntity> loadAllAdmins() {
		return userDAO.loadAllAdmins();
	}

	public List<UserEntity> loadAllOrdinaryUsers() {
		return userDAO.loadAllOrdinaryUsers();
	}

	public void update(UserEntity object, String cityName) {
		UserEntity user = userDAO.getByUsername(object.getUsername());
		user.setCity(object.getCity());
		user.setAddress(object.getAddress());
		user.setRealname(object.getRealname());
		user.setAccountType(object.getAccountType());
		user.setMobile(object.getMobile());
		user.setZipCode(object.getZipCode());
		user.setCity(cityDAO.getByChnName(cityName));
		userDAO.update(user);
	}

	public boolean checkAuthCode(String username, String uid) {
		UserEntity user = userDAO.getByUsername(username);
		if (user == null)
			return false;
		else {
			boolean flag = user.getAuthCode().equals(uid);
			if (flag) {
				user.setAccountType(Constants.USER_TYPE.USER.ordinal());
				update(user);
			}
			return flag;
		}
	}

	public void save(UserEntity object) {
		userDAO.save(object);
	}

	public void delete(UserEntity object) {
		userDAO.delete(object);
	}

	public void update(UserEntity object) {
		userDAO.update(object);
	}

	public UserEntity getById(int id) {
		return userDAO.getById(id);
	}

	public List<UserEntity> loadAll() {
		return userDAO.loadAll();
	}

	public List<UserEntity> load(int firstResult, int maxResult) {
		return userDAO.load(firstResult, maxResult);
	}

	public int getCount() {
		return userDAO.getCount();
	}
}
